package com.github.prontera.config;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.ServletContextAware;

import com.github.prontera.SpringUtils;
import com.github.prontera.service.CommonDict;
import com.github.prontera.service.TalentOrderService;
import com.github.prontera.service.TaskService;
import com.github.prontera.util.LoopRunnable;

/**
 * @description: 初始化
 */
public class InitializationListener implements ServletContextAware, ApplicationListener<ContextRefreshedEvent> {

	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		if (servletContext != null && contextRefreshedEvent.getApplicationContext().getParent() == null) {
			
			System.err.println("----------InitializationListener----------");
			CommonDict.init();
			
			SpringUtils.getBean(TalentOrderService.class).loadMemorypdateOrderStatus();
			
			LoopRunnable loopRunnable=new LoopRunnable(new Runnable() {
				@Override
				public void run() {
					SpringUtils.getBean(TalentOrderService.class).updateMemoryOrderStatus();					
//					SpringUtils.getBean(TaskService.class).schdulerUpdateTaskStart();					
//					SpringUtils.getBean(TaskService.class).schdulerUpdateEnd();					
				}
			});
			loopRunnable.setSleepInterval(5000);//5秒
			new Thread(loopRunnable).start();
		}
	}
}